// import legacy from '@vitejs/plugin-legacy';
import reactRefresh from '@vitejs/plugin-react-refresh';
import fs from 'fs';
import lessToJS from 'less-vars-to-js';
import path from 'path';
import { defineConfig } from 'vite';
import vitePluginImp from 'vite-plugin-imp';

const themeVariables = lessToJS(fs.readFileSync(path.resolve(__dirname, './theme.less'), 'utf8'));
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    reactRefresh(),
    vitePluginImp({
      libList: [
        {
          libName: 'antd',
          esModule: true,
          style: (name) => `antd/lib/${name}/style/index.less`,
        },
      ],
    }),
    // 这是开启兼容代码，是具体情况使用
    // legacy({
    //   targets: ['ie >= 11'],
    //   additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
    // }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
    extensions: ['.js', '.jsx', '.json'],
  },
  css: {
    preprocessorOptions: {
      less: {
        // 支持内联 JavaScript，支持 less 内联 JS
        javascriptEnable: true,
        // 重写 less 变量，定制样式
        modifyVars: themeVariables,
        javascriptEnabled: true,
      },
    },
    modules: {
      localsConvention: 'camelCase',
    },
  },
  server: {
    port: 3100, // 开发环境启动的端口
    proxy: {
      // '/api': {
      //   // 当遇到 /api 路径时，将其转换成 target 的值
      //   target: 'http://127.0.0.1:8080/api/',
      //   changeOrigin: true,
      //   rewrite: (pt) => pt.replace(/^\/api/, ''), // 将 /api 重写为空
      // },
    },
  },
});
